package org.gof.application.Clock
{
	import flash.events.Event;
	import flash.display.DisplayObject;
	public class FrameClock extends BaseClock
	{
		
		protected var framesAtLastPlay:int;
		
		private var _ticksPerFrame:Number;
		private var runningFrames:int;
		
		public function FrameClock(ticksPerFrame:Number,notifies:Boolean,displayObject:DisplayObject)
		{
			runningFrames = 0;
			this._ticksPerFrame = ticksPerFrame;
			super(notifies);
			if (notifies)
			{
				displayObject.addEventListener(Event.ENTER_FRAME, handleEnterFrame);
			}
		}
		
		private function handleEnterFrame(evt:Event):void
		{
			if (_playing)
			{
				tick();
			}
		}
		public override function get ticks():Number
		{
			return (frames * _ticksPerFrame);	
		}
		
		public override function play():void
		{
			framesAtLastPlay = now;
			super.play();
		}
		
		public override function pause():void
		{
			runningFrames += (now - framesAtLastPlay);
		}
		public function get ticksPerFrame():Number
		{
			return this._ticksPerFrame;
		}
		
		protected function get frames():Number
		{
			 if (_playing)
			 {
			 	return (runningFrames + (now - framesAtLastPlay));
			 } 
			return (runningFrames);		
		}
		
		
		private function get now():int
		{
			return (tickCount);
		}

	}
}